/*** SAS: CREATE THE NETWORK OF BLOCKHOLDERS IN SAS ; 

> Generates the combination of supply chain network and blockholder network positions. 
> This is an intermediate step in the creation of the network of blockholders in file 2_5_Blockholder_network(sc_blocks).do

***/
%let dir = C:/Dropbox/My Projects/0) Financial Constraints_/Data and Code;

libname mylib "&dir";

proc import datafile= "&dir./SCnetwork2.csv"
     out= SCnetwork2
     dbms=csv
     replace;
     getnames=yes;
run;
proc import datafile= "&dir./block_network.csv"
     out= block_network
     dbms=CSV
     replace;
     getnames=yes;
run;
* Combine the two networks; 
	 proc sql;
	    create table SC_blocks as
	    select distinct a.*, max(b.position) as position_2_to_1, 
	    					 max(c.position) as position_1_to_2
	    from SCnetwork2 as a 
	    	left join block_network as b on a.gvkey1=b.gvkey1 and a.gvkey2=b.gvkey2 and /* b.fyear = a.fyear  */ between a.fyear-1 and a.fyear+1
	    	left join block_network as c on a.gvkey1=c.gvkey2 and a.gvkey2=c.gvkey1 and /* c.fyear = a.fyear  */ between a.fyear-1 and a.fyear+1
	    group by a.gvkey1, a.fyear, a.gvkey2
	    order by a.gvkey1, a.fyear, a.gvkey2
		;
	quit; 
	proc export data = SC_blocks
				file = "&dir./SC_blocks.dta"
				dbms = STATA REPLACE;
	run;